//#include "ui_TankWindow.h"
#include "gamewindow.h"
#include "gameobject.h"
#include <QMouseEvent>
#include <QApplication>

GameWindow::GameWindow(QWidget *parent) :
    GWindow(parent)
    //ui(new Ui::MainWindow)
{
    //ui->setupUi(this);
    m_bMoveBody = false;

    m_scene.setSceneRect(0,0,640,480);

    /*setScene(&m_scene);
    setRenderHint(QPainter::Antialiasing);*/
    setBackgroundBrush(QPixmap(":/images/soil.png"));
    /*setCacheMode(QGraphicsView::CacheBackground);
	setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);*/
}

GameWindow::~GameWindow()
{
    //delete ui;
}

void GameWindow::keyPressEvent(QKeyEvent *event)
{
    if ( event->isAutoRepeat() )
    {
        event->ignore();
        return;
    }

//    if(event->key() == Qt::Key_Left)        g_pLight->setPos(g_pLight->pos().x()-5, g_pLight->pos().y());
//    else if(event->key() == Qt::Key_Right)  g_pLight->setPos(g_pLight->pos().x()+5, g_pLight->pos().y());
//    else if(event->key() == Qt::Key_Up)     g_pLight->setPos(g_pLight->pos().x(), g_pLight->pos().y()-5);
//    else if(event->key() == Qt::Key_Down)   g_pLight->setPos(g_pLight->pos().x(), g_pLight->pos().y()+5);
//    else                                    { event->ignore(); return; }
    switch(event->key())
    {
    case Qt::Key_M:		m_bMoveBody = true;		event->accept();	QApplication::restoreOverrideCursor();						break;
    case Qt::Key_A:     m_bMoveBody = false;	event->accept();	QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));	break;
    default:
        event->ignore();
    }
}

void GameWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
		qDebug() << "Qt::LeftButton";
        Tank* pTank = static_cast<Tank*>(GEngine::GetGame()->FindObject("Tank"));
        if(pTank)
        {
            if(m_bMoveBody)
            {
                pTank->StopAnimation();
                pTank->RotateAndMoveBodyTo(event->x(), event->y());
            }
            else
            {
                //pTank->StopAnimate();
                pTank->RotateTurretTo(event->x(), event->y());
            }
			
			event->accept();
			return;
        }
    }

    GWindow::mousePressEvent(event);
}

void GameWindow::mouseReleaseEvent(QMouseEvent *event)
{
    GWindow::mouseReleaseEvent(event);
}
